//
//  LZFMineVC.m
//  LZFBuDieJie
//
//  Created by 梁梓烽 on 16/6/21.
//  Copyright © 2016年 liangzifeng. All rights reserved.
//

#import "LZFMineVC.h"
#import "LZFMineItem.h"
#import "LZFMineCell.h"
#import <AFNetworking.h>
#import <MJExtension.h>
#import "LZFWKVC.h"
#import "LZFSettingVC.h"

#define col 4
#define margin 1
#define  itemWH (screenW-margin*(col+1))/col

@interface LZFMineVC ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(nonatomic,weak)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *mineItemArray;
@end

@implementation LZFMineVC


-(NSMutableArray *)mineItemArray
{
    if (_mineItemArray==nil) {
        _mineItemArray=[NSMutableArray new];
    }
    
    return _mineItemArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    //下载数据
    [self loadData];
    
    //设置导航条内容
    [self setUpNavigataionBar];

    //设置collectionView
    [self setUpCollectionView];
    
    

    
    self.tableView.contentInset=UIEdgeInsetsMake(-18, 0, 0, 0);
    self.tableView.sectionFooterHeight=0;
    self.tableView.backgroundColor=LZFColor(215, 215, 215);

  

}

//下载数据

-(void)loadData
{
//    "name": "审贴",
//    "icon": "http:%/%/img.spriteapp.cn%/ugc%/2015%/05%/20%/150532_5078.png",
//    "url": "mod:%/%/BDJ_To_Check",
   // http://api.budejie.com/api/api_open.php?a=square&c=topic
    
    NSMutableDictionary *dic=[NSMutableDictionary new];
    dic[@"a"]=@"square";
    dic[@"c"]=@"topic";
    
    AFHTTPSessionManager *mgr=[AFHTTPSessionManager manager];

    
    [mgr GET:@"http://api.budejie.com/api/api_open.php" parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
       NSArray *array = responseObject[@"square_list"];
       self.mineItemArray =  [LZFMineItem mj_objectArrayWithKeyValuesArray:array];
        
      
        //处理数据
        [self resolveData];
        
        
        //算collectionview的高度
        
        NSInteger count =self.mineItemArray.count;
        NSInteger row= count/col+1;
        CGFloat  height =itemWH*row;
        self.collectionView.xmg_height=height;
        
        //重新设置tableFooterView
        self.tableView.tableFooterView=self.collectionView;
        
        
        //刷新collectionview数据
        [self.collectionView reloadData];
        
        
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    
}


//算要不上多少个空白的cell--->模型数组
-(void)resolveData
{
  NSInteger count =  self.mineItemArray.count;
   
  NSInteger num=  col- (count % col);
  
    for (int i=0; i<num; i++) {
        LZFMineItem *item=[[LZFMineItem alloc]init];
        [self.mineItemArray addObject:item];
    }

}

//设置导航条内容
-(void)setUpNavigataionBar
{
    
    UIBarButtonItem *item1=[UIBarButtonItem buttonItemWith:@"mine-moon-icon" selectedImage:@"mine-moon-icon-click" target:self selector:@selector(clickMoon:)];
    
    UIBarButtonItem *item2=[UIBarButtonItem buttonItemWith:@"mine-setting-icon" highlightImage:@"mine-setting-icon-click" target:self selector:@selector(clickSetting)];
    
    self.navigationItem.rightBarButtonItems=@[item2,item1];
    
    self.navigationItem.title=@"我的";

}

-(void)clickMoon:(UIButton *)sender
{
    sender.selected=!sender.selected;
    
}

-(void)clickSetting

{
    LZFSettingVC *settingVC=[[LZFSettingVC alloc]init];
    [self.navigationController pushViewController:settingVC animated:YES];
    
}


    static NSString *ID=@"item";
//设置collectionView
-(void)setUpCollectionView
{
    
    //创建flowLayout
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing=margin;
    flowLayout.minimumLineSpacing=margin;
       flowLayout.itemSize=CGSizeMake(itemWH, itemWH);
    
    
    //创建UIConllectionView
    UICollectionView  *collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, 0, 300) collectionViewLayout:flowLayout];
    collectionView.dataSource=self;
    collectionView.delegate=self;
    collectionView.backgroundColor=LZFColor(215, 215, 215);
    collectionView.contentInset=UIEdgeInsetsMake(18, 0, 0, 0);
    
    self.tableView.tableFooterView=collectionView;
    self.collectionView=collectionView;
    
    //注册cell
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([LZFMineCell class]) bundle:nil] forCellWithReuseIdentifier:ID];
}




#pragma mark datasource 

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.mineItemArray.count;
    
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LZFMineItem *mineItem=self.mineItemArray[indexPath.row];

    
    LZFMineCell *itemCell= [collectionView dequeueReusableCellWithReuseIdentifier:ID forIndexPath:indexPath];
  
    
    itemCell.mineItem=mineItem;

    return  itemCell;
}




#pragma mark delegate 
//点击cell,push出控制器,展示网页
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    LZFMineItem *mintItem=self.mineItemArray[indexPath.row];
    
    if ( [mintItem.url containsString:@"http"]) {
        //Push页面
        LZFWKVC *wkVC=[[LZFWKVC alloc]init];
        wkVC.mineItem=mintItem;
        [self.navigationController pushViewController:wkVC animated:YES];
        
    }
   
    
}



















@end
